package com.kzw.misc.web.servlet;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;

import com.kzw.misc.utils.AppUtil;
import com.kzw.oa.system.model.FileAttach;
import com.kzw.oa.system.service.FileAttachService;

public class FileDownloadServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private FileAttachService fileAttachService = (FileAttachService) AppUtil.getBean("fileAttachService");

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		String fileId = req.getParameter("fileId");
		req.setCharacterEncoding("UTF-8");
		resp.setCharacterEncoding("UTF-8");

		if (StringUtils.isNotEmpty(fileId)) {
			FileAttach fileAttach = fileAttachService.get(new Long(fileId));
			String ext = fileAttach.getExt();

			if (ext.toLowerCase().endsWith("zip")) {
				resp.setContentType("application/x-zip-compressed");
			} else if (ext.toLowerCase().endsWith("rar")) {
				resp.setContentType("application/octet-stream");
			} else if (ext.toLowerCase().endsWith("doc")) {
				resp.setContentType("application/msword");
			} else if (ext.toLowerCase().endsWith("xls") || ext.toLowerCase().endsWith("csv")) {
				resp.setContentType("application/ms-excel ");

			} else if (ext.toLowerCase().endsWith("pdf")) {
				resp.setContentType("application/pdf");
			} else {
				resp.setContentType("application/x-msdownload");
			}

			ServletOutputStream out = null;
			try {

				String base = getServletContext().getRealPath("/");
				InputStream in = new FileInputStream(base + "/attachFiles/" + fileAttach.getFilePath());

				resp.setHeader("Content-Disposition",
						"attachment;filename=" + URLEncoder.encode(fileAttach.getFileName(), "UTF-8"));

				out = resp.getOutputStream();

				byte[] buff = new byte[1024];
				int leng = in.read(buff);
				while (leng > 0) {
					out.write(buff, 0, leng);
					leng = in.read(buff);
				}

				in.close();
				out.flush();
				out.close();
			} catch (Exception ex) {
				ex.printStackTrace();
			}

		}

	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}
}
